import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class NotAutoCommit{
    public static void main(String[] args){
        Connection conn = null;
        Statement stmt = null;
        
        try{
            
            Class.forName("com.mysql.jdbc.Driver");
            
            String url = "jdbc:mysql://localhost:3306/test?useSSL=false";
            String username = "root";
            String password = "";
            conn = DriverManager.getConnection(url, username, password);
            
            stmt = conn.createStatement();
            
            String sql1 = "update stu set id = -1 where id = 1";
            String sql2 = "update stu set state = 'hei!' where id = 1";
            
            conn.setAutoCommit(false);
            
            stmt.executeUpdate(sql1);
            stmt.executeUpdate(sql2);
            
        }catch(Exception e){
            System.out.println("哈哈哈");
            try{
                conn.rollback();
            }catch(Exception e2){
                e2.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            try{
                conn.commit();
                stmt.close();
                conn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}